How to read an array from a include file

I am trying to read an array from a include file and then load the array to a skiplist. I can read the file but don't know how to loop thru the array from a inc file.

// <#include file.inc>
string filepath = "\\c:\\temp\\file.inc"
string array MstrGtslst[]={"A","B","C"} // this info is in the file.inc
stream input =read filepath
Skip SKgatedates=createstring()

i = 0
for (i=0; i<(sizeof(MstrGtslst)); i++){
put(SKgatedates, MstrGtslst[i] , sGatesdates[i])
}

Any suggestion is appreciated

fz
faisal.zahidi@boeing.com - Thu Aug 09 16:18:09 EDT 2012

Re: How to read an array from a include file
llandale - Thu Aug 09 16:50:44 EDT 2012

Get rid of the word "array"
  • string MstrGtslst[]={"A","B","C"} // this info is in the file.inc
or better:
  • string array MstrGtslst[3] // this info is in the file.inc

Let's not confuse a native "array" (lower case "a") which can be applied to any data type
  • DB dbMany // 'singular' DB
  • DB dbMany[4] // 'array' of DBs
with an "Array" (upper case "A") which is its own data type, and can house any other data type.
  • Array arr = create(1,1)

-Louie

Re: How to read an array from a include file
faisal.zahidi@boeing.com - Fri Aug 10 17:47:47 EDT 2012

Louie,

Got it!. I accidently typed the word "array" in the attached sample code. but that was not my question...

But I just want to know how to loop thru an array by reading that array from a .inc file. (I know how to loop thru any string array if it is in the code but in a .inc file)

so far I know how to read the file...
string filepath = "\\c:\\temp\\file.inc"
string gatedate = "\\c:\\temp\\date.inc"

....
Stream input = read filepath
Stream input1 = read gatedate

string MstrGtslst[]={"A","B","C"} //this contains in the file.inc
string gatedate[]={"3/3/12","6/4/13","7/8/14"} //this contains in the date.inc

Skip SKgatedates=createstring()
i = 0
for (i=0; i<(sizeof(MstrGtslst)); i++){
put(SKgatedates, MstrGtslst[i] , gatedate[i])
}
Now i cannot loop thru the MstrGtslst[]

The sample code i have attached is giving me an error message that MstrGtslst and gatedate are undeclare...but they declared in the .inc file
My end goal is to read two arrays from two different files and load each item into a skiplist as a key and a value.
Thanks,

FZ

Re: How to read an array from a include file
Mathias Mamsch - Fri Aug 10 18:23:56 EDT 2012

faisal.zahidi@boeing.com - Fri Aug 10 17:47:47 EDT 2012
Louie,

Got it!. I accidently typed the word "array" in the attached sample code. but that was not my question...

But I just want to know how to loop thru an array by reading that array from a .inc file. (I know how to loop thru any string array if it is in the code but in a .inc file)

so far I know how to read the file...
string filepath = "\\c:\\temp\\file.inc"
string gatedate = "\\c:\\temp\\date.inc"

....
Stream input = read filepath
Stream input1 = read gatedate

string MstrGtslst[]={"A","B","C"} //this contains in the file.inc
string gatedate[]={"3/3/12","6/4/13","7/8/14"} //this contains in the date.inc

Skip SKgatedates=createstring()
i = 0
for (i=0; i<(sizeof(MstrGtslst)); i++){
put(SKgatedates, MstrGtslst[i] , gatedate[i])
}
Now i cannot loop thru the MstrGtslst[]

The sample code i have attached is giving me an error message that MstrGtslst and gatedate are undeclare...but they declared in the .inc file
My end goal is to read two arrays from two different files and load each item into a skiplist as a key and a value.
Thanks,

FZ

Well if the include file is syntactically correct and parsable on its own, you can use an eval_ trick to read the elements. But you should test this well. The strings of the DXL file could be freed when returning to the original context, so if you get crashes then you need to feed them to a buffer instead or something. Regards, Mathias
 

// put your include in the code
 
string sCode = "
string arTest[] = {\"This\", \"is\", \"fun!\"}
int arTest2[] = {7,8,9}
"
 
 
Skip readArrayFromInclude (string sInclude, string sArrayName, string sArrayType) {
    Skip skResult = create() 
 
        // This code will marshall the array values to the skip list outside the eval_ context
        string sSkipCode = sInclude "
Skip sk = addr_ " ((addr_ skResult) int) "
int i; for (i = 0; i < sizeof " sArrayName "; i++) put(sk, i, " sArrayName "[i])
"
 
        // print sSkipCode
      eval_ sSkipCode
        return skResult
}
 
Skip sk1 = readArrayFromInclude (sCode, "arTest", "string")
Skip sk2 = readArrayFromInclude (sCode, "arTest2", "int")
 
print "String values: " 
string sValue; for sValue in sk1 do print sValue " "
print "\n"
 
print "Int values: " 
int iValue; for iValue in sk2 do print iValue " " 
print "\n"

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: How to read an array from a include file
llandale - Sat Aug 11 12:02:06 EDT 2012

faisal.zahidi@boeing.com - Fri Aug 10 17:47:47 EDT 2012
Louie,

Got it!. I accidently typed the word "array" in the attached sample code. but that was not my question...

But I just want to know how to loop thru an array by reading that array from a .inc file. (I know how to loop thru any string array if it is in the code but in a .inc file)

so far I know how to read the file...
string filepath = "\\c:\\temp\\file.inc"
string gatedate = "\\c:\\temp\\date.inc"

....
Stream input = read filepath
Stream input1 = read gatedate

string MstrGtslst[]={"A","B","C"} //this contains in the file.inc
string gatedate[]={"3/3/12","6/4/13","7/8/14"} //this contains in the date.inc

Skip SKgatedates=createstring()
i = 0
for (i=0; i<(sizeof(MstrGtslst)); i++){
put(SKgatedates, MstrGtslst[i] , gatedate[i])
}
Now i cannot loop thru the MstrGtslst[]

The sample code i have attached is giving me an error message that MstrGtslst and gatedate are undeclare...but they declared in the .inc file
My end goal is to read two arrays from two different files and load each item into a skiplist as a key and a value.
Thanks,

FZ

You say the contents of file.inc is this single line??:
  • string MstrGtslst[]={"A","B","C"}
I think you should just include it instead of reading it:
  • #include <c:\temp\file.inc>
You will not be able to do that if the named file is some sort of variable.

Your code right now opens the file and gets a Stream handle, but does not read it. It does not make sense to me to read that line, parse it, find the 3 values, and store them in a Skip.

As for Mathias' solution, I think your main code will need this:
  • string MasterList = readFile(filepath) // Read the entire contents without bothering to open/read/close the file.
Seems to me I recall an issue when the file contains a zero byte, which is interpreted as null and end of file. So I think readFile() only works on ascii files.

-Louie

Re: How to read an array from a include file
faisal.zahidi@boeing.com - Mon Aug 13 12:00:00 EDT 2012

llandale - Sat Aug 11 12:02:06 EDT 2012
You say the contents of file.inc is this single line??:

  • string MstrGtslst[]={"A","B","C"}
I think you should just include it instead of reading it:
  • #include <c:\temp\file.inc>
You will not be able to do that if the named file is some sort of variable.

Your code right now opens the file and gets a Stream handle, but does not read it. It does not make sense to me to read that line, parse it, find the 3 values, and store them in a Skip.

As for Mathias' solution, I think your main code will need this:
  • string MasterList = readFile(filepath) // Read the entire contents without bothering to open/read/close the file.
Seems to me I recall an issue when the file contains a zero byte, which is interpreted as null and end of file. So I think readFile() only works on ascii files.

-Louie

Louie,
I am completely lost in the Mathias' code because...

the contents of the file.inc is not a single line (more than 3 lines and 50 values) and the two arrays are in two different file1.inc and file2.inc files (array sizes are same).

And I want to load the first array data as a key and the second array data as a value in the same skiplist.
And the name files are variables. "\\sw\temp\file1.inc" and "\\sw\temp\file1.inc" (files reside on the share drive).
if needed, I have the flexability to change the contain format from

•string MstrGtslst[]={"A","B","C".......} //file1 values
to {"A","B","C"............} ONLY

•string datelst[]={"06/23/2011","06/23/2011","07/11/2013".....} //file2 values
to {"06/23/2011","06/23/2011","07/11/2013".....}

Any suggestion is appreciated.

FZ

Re: How to read an array from a include file
llandale - Mon Aug 13 13:12:26 EDT 2012

faisal.zahidi@boeing.com - Mon Aug 13 12:00:00 EDT 2012
Louie,
I am completely lost in the Mathias' code because...

the contents of the file.inc is not a single line (more than 3 lines and 50 values) and the two arrays are in two different file1.inc and file2.inc files (array sizes are same).

And I want to load the first array data as a key and the second array data as a value in the same skiplist.
And the name files are variables. "\\sw\temp\file1.inc" and "\\sw\temp\file1.inc" (files reside on the share drive).
if needed, I have the flexability to change the contain format from

•string MstrGtslst[]={"A","B","C".......} //file1 values
to {"A","B","C"............} ONLY

•string datelst[]={"06/23/2011","06/23/2011","07/11/2013".....} //file2 values
to {"06/23/2011","06/23/2011","07/11/2013".....}

Any suggestion is appreciated.

FZ

I'm lost in the Mathias' code as well, but he's only made one mistake so I think you should go for it. He's turning the string version of the code into executable code using eval_(). You should
  • print ">>>sCode>>>\n" sSkipCode // this will help see what is going on
  • string Code1 = readFile(NameFile1)
  • string Code2 = readFile(NameFile2)
  • use Code1 and Code2 instead of sCode parameter to readArrayFromInclude().

As for writing a CSV parser, I'm not going there.

-Louie

Re: How to read an array from a include file
Mathias Mamsch - Mon Aug 13 16:27:07 EDT 2012

llandale - Mon Aug 13 13:12:26 EDT 2012
I'm lost in the Mathias' code as well, but he's only made one mistake so I think you should go for it. He's turning the string version of the code into executable code using eval_(). You should

  • print ">>>sCode>>>\n" sSkipCode // this will help see what is going on
  • string Code1 = readFile(NameFile1)
  • string Code2 = readFile(NameFile2)
  • use Code1 and Code2 instead of sCode parameter to readArrayFromInclude().

As for writing a CSV parser, I'm not going there.

-Louie

... but he's only made one mistake ...

 


You mean like "ever"? ;-) I am not really sure I made a mistake in my code. In my code I put some test code in that string, but it could also be a couple of include statements. Like:

 

 

 

string sCode = "#include <myFile.inc>\n"



or



 

 

 

 

string sCode = "#include <">\n"



So there is no need to use readFile, as long as the include file is parsable and executable. Regards, Mathias



 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: How to read an array from a include file
faisal.zahidi@boeing.com - Mon Aug 13 19:23:58 EDT 2012

Mathias Mamsch - Mon Aug 13 16:27:07 EDT 2012

... but he's only made one mistake ...

 


You mean like "ever"? ;-) I am not really sure I made a mistake in my code. In my code I put some test code in that string, but it could also be a couple of include statements. Like:

 

 

 

string sCode = "#include <myFile.inc>\n"



or



 

 

 

 

string sCode = "#include <">\n"



So there is no need to use readFile, as long as the include file is parsable and executable. Regards, Mathias



 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Mathias,

The include file is on the following location

c:\temp\myfile.inc

And I edit your code to

string sCode = "#include <c:\temp\myFile.inc>\n"

And got the following error.

-E- DXL: <Line:2> badly formed token ("#include)
-E- DXL: <Line:2> incorrect argument for (<_)
-E- DXL: <Line:2> undeclared variable (c)
what I am doing wrong....
fz

Re: How to read an array from a include file
Mathias Mamsch - Tue Aug 14 08:42:10 EDT 2012

faisal.zahidi@boeing.com - Mon Aug 13 19:23:58 EDT 2012
Mathias,

The include file is on the following location

c:\temp\myfile.inc

And I edit your code to

string sCode = "#include <c:\temp\myFile.inc>\n"

And got the following error.

-E- DXL: <Line:2> badly formed token ("#include)
-E- DXL: <Line:2> incorrect argument for (<_)
-E- DXL: <Line:2> undeclared variable (c)
what I am doing wrong....
fz

Well this is a parser error, which should hint you that your syntax is wrong. You need to escape backslashes or use forward slashes:
 

string sCode = "#include <c:\\temp\\myFile.inc>\n"

 


or

 

 

string sCode = "#include <c:/temp/myFile.inc>\n"



Regards, Mathias



 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: How to read an array from a include file
faisal.zahidi@boeing.com - Tue Aug 14 13:49:00 EDT 2012

Mathias Mamsch - Tue Aug 14 08:42:10 EDT 2012

Well this is a parser error, which should hint you that your syntax is wrong. You need to escape backslashes or use forward slashes:
 

string sCode = "#include <c:\\temp\\myFile.inc>\n"

 


or

 

 

string sCode = "#include <c:/temp/myFile.inc>\n"



Regards, Mathias



 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Mathias,

Your code works for two separate skiplists.
My end goal is to load both data to a single skiplist as a key value and data. My approach is as follows:

string sCode1 = "#include <//sw/sb-data1/ent-doors-sw-dxl/projectaddins/ELS-Standard/include/MEXSAT/GATES.inc>\n"
string sCode2 = "#include <//sw/sb-data1/ent-doors-sw-dxl/projectaddins/ELS-Standard/include/MEXSAT/GATE_DATE.inc>\n"
Skip readArrayFromIncludeGates (string sInclude, string sArrayName, string sArrayType,string sInclude1, string sArrayName1, string sArrayType1) {
Skip skResult = create()

// This code will marshall the array values to the skip list outside the eval_ context
string sSkipCode = sInclude "
Skip sk = addr_ " ((addr_ skResult) int) "
int i; for (i = 0; i < sizeof " sArrayName "; i++) put(sk, "sArrayName "[i], " sArrayName1 "[i])
"

// print sSkipCode
eval_ sSkipCode
return skResult
}

Skip sk1 = readArrayFromIncludeGates (sCode1, "GATES", "string",sCode2, "GATE_DATE","string")
print "String values: "
string sValue; for sValue in sk1 do print sValue " "
print "\n"

I believe that the second file is not accessable in the above approach. I am getting the following error message. what I am doing wrong.
-E- DXL: <Line:4> incorrect arguments for ([)
-E- DXL: <Line:4> incorrect arguments for (for)
-E- DXL: <Line:4> incorrect arguments for function (put)
-E- DXL: <Line:4> undeclared variable (GATE_DATE)

Re: How to read an array from a include file
llandale - Tue Aug 14 14:06:22 EDT 2012

Mathias Mamsch - Mon Aug 13 16:27:07 EDT 2012

... but he's only made one mistake ...

 


You mean like "ever"? ;-) I am not really sure I made a mistake in my code. In my code I put some test code in that string, but it could also be a couple of include statements. Like:

 

 

 

string sCode = "#include <myFile.inc>\n"



or



 

 

 

 

string sCode = "#include <">\n"



So there is no need to use readFile, as long as the include file is parsable and executable. Regards, Mathias



 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

"ever" in the 2+ years you've been here.

Re: How to read an array from a include file
Mathias Mamsch - Tue Aug 14 14:07:54 EDT 2012

faisal.zahidi@boeing.com - Tue Aug 14 13:49:00 EDT 2012
Mathias,

Your code works for two separate skiplists.
My end goal is to load both data to a single skiplist as a key value and data. My approach is as follows:

string sCode1 = "#include <//sw/sb-data1/ent-doors-sw-dxl/projectaddins/ELS-Standard/include/MEXSAT/GATES.inc>\n"
string sCode2 = "#include <//sw/sb-data1/ent-doors-sw-dxl/projectaddins/ELS-Standard/include/MEXSAT/GATE_DATE.inc>\n"
Skip readArrayFromIncludeGates (string sInclude, string sArrayName, string sArrayType,string sInclude1, string sArrayName1, string sArrayType1) {
Skip skResult = create()

// This code will marshall the array values to the skip list outside the eval_ context
string sSkipCode = sInclude "
Skip sk = addr_ " ((addr_ skResult) int) "
int i; for (i = 0; i < sizeof " sArrayName "; i++) put(sk, "sArrayName "[i], " sArrayName1 "[i])
"

// print sSkipCode
eval_ sSkipCode
return skResult
}

Skip sk1 = readArrayFromIncludeGates (sCode1, "GATES", "string",sCode2, "GATE_DATE","string")
print "String values: "
string sValue; for sValue in sk1 do print sValue " "
print "\n"

I believe that the second file is not accessable in the above approach. I am getting the following error message. what I am doing wrong.
-E- DXL: <Line:4> incorrect arguments for ([)
-E- DXL: <Line:4> incorrect arguments for (for)
-E- DXL: <Line:4> incorrect arguments for function (put)
-E- DXL: <Line:4> undeclared variable (GATE_DATE)

Use the original code to read the values in two different skips. Then intermingle them using the following code:
 

string sGateVal, sDateVal; 
Skip result = createString() 
 
Skip skGates = readArrayFromInclude ("#include <...GATE_DATE.inc>\n", "GATES", "string")
Skip skDates = readArrayFromInclude ("#include <...GATES.inc>\n", "GATE_DATE", "string")
 
// iterate through gate skip
for sGateVal in skGates do {
    // get index in skip
    int iKey = (key skGates) int; 
    // find date value in other skip and put to result
    if (find (skDates, iKey, sDateVal)) put (result, sDateVal "", sGateVal "") // make a copy of the strings here to be sure they stay alive ;-)
}
 
delete skGates; skGates = null 
delete skDates; skDates = null

 


Hope this helps, regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: How to read an array from a include file
faisal.zahidi@boeing.com - Tue Aug 14 19:49:27 EDT 2012

Works!!. Thanks a lot Mathias.